package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.apache.ibatis.session.SqlSession;

import java.awt.image.ImageProducer;
import java.time.LocalDate;
import java.util.List;

public class ProductServiceImpl extends BaseService implements IProductService {
    @Override
    public int add(ProductBean product) {
        SqlSession session = this.getSession();
        IProductMapper mapper =session.getMapper(IProductMapper.class);
        mapper.add(product);
        session.commit();
        session.close();
        return product.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IProductMapper mapper =session.getMapper(IProductMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, int price) {
        SqlSession session = this.getSession();
        IProductMapper mapper =session.getMapper(IProductMapper.class);
        mapper.update(id,price);
        session.commit();
        session.close();
    }

    @Override
    public ProductBean findById(int id) {
        SqlSession session = this.getSession();
        IProductMapper mapper =session.getMapper(IProductMapper.class);
        ProductBean product=mapper.findById(id);

        session.close();
        return product;
    }

    @Override
    public CutPageBean<ProductBean> findByItem(String factory, int price, int pageNO) {
        CutPageBean<ProductBean> cutBean = new CutPageBean<>();
        SqlSession session =this.getSession();
        IProductMapper mapper =session.getMapper(IProductMapper.class);
        List<ProductBean> list=mapper.findByItem(factory,price,
                (pageNO-1)*CutPageBean.pageSize,CutPageBean.pageSize);
        cutBean.setList(list);
        cutBean.setNum(mapper.countByItem(factory,price));
        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<ProductBean> cutByDate(String factory, LocalDate start, LocalDate end, int pageNO) {
        CutPageBean<ProductBean> cutBean = new CutPageBean<>();
        SqlSession session =this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.cutByDate(factory,start,end,
                (pageNO-1)*CutPageBean.pageSize,CutPageBean.pageSize);
        cutBean.setList(list);
        cutBean.setNum(mapper.countByDate(factory,start,end));
        session.close();
        return cutBean;
    }


}
